<!--
  #%L
  thinkbig-ui-feed-manager
  %%
  Copyright (C) 2017 ThinkBig Analytics
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->
<card-layout body-css="md-padding">
  <header-section>
    <div layout="row" layout-align="space-between-center" flex>
      <div class="card-title" flex>Schedule</div>

      <div class="card-sub-header">{{vm.stepNumber}} of {{vm.totalSteps}}</div>
    </div>
    <div layout="row" layout-align="space-between-center" flex class="layout-padding-top">
      <div class="card-sub-header" flex>Schedule and enable the feed</div>
      <thinkbig-feed-errors-card-header></thinkbig-feed-errors-card-header>
    </div>
  </header-section>
  <body-section md-content>
    <ng-form name="vm.defineFeedScheduleForm">
      <div layout-gt-sm="row" style="margin-bottom: 18px">

        <md-input-container class="md-block" flex>
          <label>Strategy</label>
          <md-select ng-model="vm.model.schedule.schedulingStrategy" ng-change="vm.onScheduleStrategyChange()" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.schedulingStrategy.disabled">
            <md-option ng-value="opt.value" ng-repeat="opt in vm.scheduleStrategies">{{ opt.label }}</md-option>
          </md-select>
        </md-input-container>

        <md-input-container class="md-block" flex ng-if="vm.model.schedule.schedulingStrategy == 'CRON_DRIVEN'">
          <label>Expression</label>
          <input required ng-model="vm.model.schedule.schedulingPeriod" name="cronExpression" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.schedulingPeriod.disabled"
                 cron-expression-validator
                 ng-model-options="{ updateOn: 'default blur', debounce: {'default': 300, 'blur': 0} }"/>

          <div ng-show="vm.defineFeedScheduleForm.cronExpression.$touched && vm.defineFeedScheduleForm.cronExpression.$invalid"
               ng-messages="vm.defineFeedScheduleForm.cronExpression.$error">
            <div ng-show="vm.defineFeedScheduleForm.cronExpression.$error.cronExpression"
                 ng-message="cronExpression">Invalid cron expression.
            </div>
            <div ng-show="vm.defineFeedScheduleForm.cronExpression.$error.required" ng-message="required">
              Required.
            </div>
          </div>
          <cron-expression-preview cron-expression="vm.model.schedule.schedulingPeriod"
                                   ng-if="vm.model.schedule.schedulingStrategy == 'CRON_DRIVEN' && vm.defineFeedScheduleForm.cronExpression.$valid"></cron-expression-preview>
        </md-input-container>
        <div flex ng-if="vm.model.schedule.schedulingStrategy == 'TIMER_DRIVEN' || vm.model.schedule.schedulingStrategy == 'PRIMARY_NODE_ONLY'" layout="row" style="margin-bottom:-18px">
          <md-input-container class="md-block" flex>
            <label>Run Schedule</label>
            <input ng-model="vm.timerAmount" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.schedulingStrategy.disabled" required string-to-number type="number" ng-change="vm.timerChanged()"  ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }"/>
          </md-input-container>

          <md-input-container class="md-block" flex >
            <label>Units</label>
            <md-select ng-model="vm.timerUnits" ng-change="vm.timerChanged()" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.schedulingStrategy.disabled">
              <md-option value="days">Days</md-option>
              <md-option value="hrs">Hours</md-option>
              <md-option value="min">Minutes</md-option>
              <md-option value="sec">Seconds</md-option>
            </md-select>
          </md-input-container>
        </div>

      </div>
      <!--
          <md-input-container class="md-block" >
              <label>Concurrent Tasks</label>
              <input type="number" ng-model="vm.model.schedule.concurrentTasks">
              </input>
          </md-input-container>
          -->
      <div ng-if="vm.isClustered && vm.supportsExecutionNode" style="margin:18px 0">
        <md-input-container class="md-block">
          <label>Execution Node</label>
          <md-select ng-model="vm.model.schedule.executionNode" required ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.executionNode.disabled">
            <md-option value="ALL">All nodes</md-option>
            <md-option value="PRIMARY">Primary node</md-option>
          </md-select>
        </md-input-container>
      </div>

      <div ng-if="vm.model.schedule.schedulingStrategy == 'TRIGGER_DRIVEN'">
        <div layout="column">
          <div class="md-subhead">Run Preconditions</div>
          <div class="hint">Specify execution dependencies</div>
        </div>
        <div layout="column" layout-align="start start" class="layout-padding-top-bottom">

          <div ng-if="vm.model.schedule.preconditions != null && vm.model.schedule.preconditions.length >0 " layout-fill>
            <md-list>
              <md-list-item ng-repeat="rule in vm.model.schedule.preconditions track by $index" layout-fill class="layout-padding-top-bottom">
                <div layout="column" flex class="md-list-item-text">
                  <span>{{rule.propertyValuesDisplayString}}</span>
                  <span class="hint">{{rule.name}}</span>
                </div>
                            <span class="md-secondary">
                                  <md-button class="md-icon-button" ng-click="vm.showPreconditionDialog($index)">
                                    <ng-md-icon icon="edit"></ng-md-icon>
                                  </md-button>
                            </span>
                <md-divider ng-if="!$last"></md-divider>
              </md-list-item>
            </md-list>
          </div>
          <md-button class="md-primary" ng-click="vm.showPreconditionDialog()" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.preconditions.disabled">
            Add Precondition
          </md-button>
        </div>

      </div>

      <div layout="column">
        <md-checkbox ng-model="vm.model.active" style="margin-top:18px" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.active.disabled"> Enable feed immediately</md-checkbox>
        <span class="hint fade" ng-show="vm.model.active" ng-hide="!vm.model.active">The feed will be scheduled and running when it is saved.</span>
        <span class="hint fade" ng-show="!vm.model.active" ng-hide="vm.model.active">The feed will be disabled when it is saved.  You will need to enable the feed later.</span>
      </div>


      <thinkbig-step-buttons can-continue="vm.isValid && vm.defineFeedScheduleForm.$valid &&  !vm.savingFeed" step-index="{{::vm.stepIndex}}" final-step="Create" on-click-final-button="vm.createFeed()"></thinkbig-step-buttons>

    </ng-form>
  </body-section>
</card-layout>

